﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;

namespace HmmmIDE.Syntax
{
    public class SyntaxManager
    {
        /// <summary>
        /// Command description table
        /// </summary>
        public static Dictionary<string, string> CommandDescription;
        /// <summary>
        /// Command usage table
        /// </summary>
        public static Dictionary<string, string> CommandUsage;
        /// <summary>
        /// Command regular expression table
        /// </summary>
        public static Dictionary<string, string> CommandRegex;

        /// <summary>
        /// Load the syntax detail from the XML
        /// </summary>
        public static void Load()
        {
            CommandDescription = new Dictionary<string, string>();
            CommandUsage = new Dictionary<string, string>();
            CommandRegex = new Dictionary<string, string>();

            string name = "";
            string descpiption = "";
            string usage = "";
            string regex = "";
            string xmlName = "";
            using (Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream("HmmmIDE.Syntax.Command.xml"))
            {
                XmlTextReader reader = new XmlTextReader(s);

                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        xmlName = reader.Name;
                    }
                    else if (reader.NodeType == XmlNodeType.Text)
                    {
                        if (xmlName == "Name")
                        {
                            name = reader.Value;
                        }
                        else if (xmlName == "Description")
                        {
                            descpiption = reader.Value;
                        }
                        else if (xmlName == "Usage")
                        {
                            usage = reader.Value;
                        }
                        else if (xmlName == "Syntax")
                        {
                            regex = reader.Value;
                            // Add the dictionary
                            CommandDescription.Add(name, descpiption);
                            CommandUsage.Add(name, usage);
                            CommandRegex.Add(name, regex);

                        }

                    }
                }
            }
        }
    }
}
